Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList: [],
    chooseaddr: 1, //1代表从“我的”页面中的收货地址进入，0代表从订单页面过来
    buynow: 0,
    nowbuydetail:{},//立即购买时传过来的商品信息
    Mstart: '', //记录左滑开始时的位置
    slideIndex: '', //记录左滑的元素
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let that = this
    if (options) {
      that.setData({
        chooseaddr: options.chooseaddr,
        buynow: options.buynow
      })
    }
    if(options.detail){
      that.setData({
        nowbuydetail: JSON.parse(options.detail)
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    wx.getStorage({
      key: 'addresslist',
      success: function(res) {
        that.setData({
          addressList: res.data
        })
      },
    })
  },
  //新增收货地址
  addAddress: function() {
    let that = this
    wx.redirectTo({
      url: '/pages/address/address?chooseaddr=' + that.data.chooseaddr + '&isNewOne=' + 1 + '&buynow=' + that.data.buynow + '&detail=' + JSON.stringify(that.data.nowbuydetail)
    });
  },

  //触摸开始
  touchstart: function(e) {
    console.log(e)
    this.setData({
      slideIndex: e.currentTarget.dataset.index,
      Mstart: e.changedTouches[0].pageX
    });
  },
  //开始滑动
  touchmove: function(e) {
    //列表项数组
    console.log(e.changedTouches[0].pageX)
    let list = this.data.addressList;
    let move = this.data.Mstart - e.changedTouches[0].pageX;
    console.log(move)
    list[this.data.slideIndex].x = move > 0 ? -move : 0;
    this.setData({
      addressList: list
    });
  },
  //滑动结束
  touchend: function(e) {
    let list = this.data.addressList;
    console.log(e.changedTouches[0].pageX)
    let move = this.data.Mstart - e.changedTouches[0].pageX;
    list[this.data.slideIndex].x = move > 80 ? -120 : 0;
    this.setData({
      addressList: list
    });
  },
  /* 删除地址 */
  delAddress: function(e) {
    let that = this
    this.data.addressList.splice(e.target.dataset.index, 1);
    this.setData({
      addressList: that.data.addressList
    })
    wx.setStorage({
      key: 'addresslist',
      data: this.data.addressList,
    })
  },

  //编辑地址
  editor: function(e) {
    console.log(e)
    let that = this
    let address = e.target.dataset
    console.log(address)
    let data = {
      'name': address.name,
      'tel': address.tel,
      'city': address.city,
      'detail': address.detail
    }
    wx.navigateTo({
      url: '/pages/address/address?chooseaddr=' + that.data.chooseaddr + '&data=' + JSON.stringify(data) + '&isNewOne=' + 0 + '&index=' + address.index + '&buynow=' + that.data.buynow,
    })
  },
  // 确认订单页过来，可以点击进行选择地址，“我的”页面不可以点击
  clickme: function(e) {
    let that = this
    let choiceaddr = e.currentTarget.dataset
    console.log(e)
    console.log(that.data.chooseaddr)
    if (that.data.chooseaddr == 0) {
      wx.setStorage({
        key: 'choiceaddres',
        data: choiceaddr.addres,
      })
      if (that.data.buynow == 1) {
        wx.getStorage({
          key: 'nowbuydetail',
          success: function (res) {
            wx.navigateTo({
              url: '../../pages/confirmorder/confirmorder?buynow=' + that.data.buynow + '&detail=' + JSON.stringify(res.data),
            })
          },
        })
      } else {
        wx.navigateTo({
          url: '../../pages/confirmorder/confirmorder?buynow=' + that.data.buynow,
        })
      }
    }
  },
    //订单详情页回到首页
  backhome: function () {
    wx.switchTab({
      url: '../../pages/home/home',
    })
  },
})